package com.Modular.pdf_Util.examplesPDF.CheLiang.CheLiangXingCheRiZhiDengJi;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import com.Modular.MinioPlugin.MinIoOperations;
import com.Modular.pdf_Util.PDF_utils;
import com._Config.Model.errorLogModel;
import com.jfinal.core.Controller;
import com.jfinal.log.Log4jLog;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 车辆行车日志登记
 */
public class CheLiangXingCheRiZhiDengJiService extends Controller {
    static Log4jLog logger= Log4jLog.getLog(CheLiangXingCheRiZhiDengJiService.class);

    public static final CheLiangXingCheRiZhiDengJiService service = new CheLiangXingCheRiZhiDengJiService();

    public String buildPdf(String id) {

        try {

        	errorLogModel model=
        			errorLogModel.dao.findById(id);
            if(id==null|| model==null){
                throw new RuntimeException("Id 未查到数据");
            }
            Map<String,String> titleMap=new HashMap<>();
            titleMap=xingCheRiZhiToMap(model);
            String time=DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm");
            titleMap.put("Time",time);

            String dir=System.getProperty("user.dir");
            String fileName=String.format("%s%s.pdf","车辆行车日志登记表", DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            String outPath = String.format("%s%s%s",dir,"/webapp/WEB-INF/template/temp/",fileName);

            String pdfName = String.format("%s.pdf","车辆行车日志登记表-"+time);
             //生成本地pdf 一表单+一表格
            PDF_utils.buildPdf("车辆行车日志登记表-模板.docx",outPath,titleMap);
            logger.debug("fileName:%s,output:%s",fileName,outPath);
            MinIoOperations.fileUpload("asiatrip", pdfName,outPath);
            String geturl = MinIoOperations.fileGeturlGet("asiatrip", pdfName);

            String SYS_DanWeiMingCheng=model.getStr("SYS_DanWeiMingCheng");
            String danWeiDaiMa=model.getStr("SYS_DanWeiDaiMa");
            //关连电子档案
            dangAnWenJian("车辆行车日志登记表",pdfName,geturl,null,SYS_DanWeiMingCheng,danWeiDaiMa);
            return geturl;
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public Map<String, String> xingCheRiZhiToMap(errorLogModel model){
        Map<String, String> data = new HashMap<>();
        data.put("QiYeMingCheng",model.getStr("QiYeMIngCheng"));
        data.put("ChePaiHaoMa",model.getStr("ChePaiHaoMa"));
        data.put("ChePaiYanSe", model.getStr("ChePaiYanSe"));
        data.put("JiaShiYuan", model.getStr("JiaShiYuan"));
        data.put("ChuCheRiQi", DateUtil.parse(model.getStr("ChuCheRiQi")).toString("yyyy-MM-dd HH:mm"));
        data.put("ShiFaDian",model.getStr("ShiFaDian"));
        data.put("ZhongDaoDian",model.getStr("ZhongDaoDian"));
        data.put("ZhuangZaiWuMingCheng",model.getStr("ZhuangZaiWuMingCheng"));
        data.put("ZhuangZaiWuXingZhi",model.getStr("ZhuangZaiWuXingZhi"));
        data.put("ZhuangZaiWuShuLiang",model.getStr("ZhuangZaiWuShuLiang"));
        data.put("DiaoDuYuan",model.getStr("DiaoDuYuan"));
        data.put("FaCheShiJian", DateUtil.parse(model.getStr("FaCheShiJian")).toString("yyyy-MM-dd HH:mm"));
        data.put("ZhongDian",model.getStr("ZhongDian"));
        data.put("TianQiZhuangKuang",model.getStr("TianQiZhuangKuang"));
        data.put("DaoDaShiJian",DateUtil.parse(model.getStr("DaoDaShiJian")).toString("yyyy-MM-dd HH:mm"));
        data.put("XingShiLiCheng",model.getStr("XingShiLiCheng"));
        data.put("XingShiLuXian",model.getStr("XingShiLuXian"));
        data.put("ShiFouJiaYou",model.getStr("ShiFouJiaYou"));
        data.put("JiaYouDiDian",model.getStr("JiaYouDiDian"));
        data.put("TingCheXiuXIZhuangKuang",model.getStr("TingCheXiuXIZhuangKuang"));
        data.put("XingCheTuZhongCheLiangJiShuZhuangKuang",model.getStr("XingCheTuZhongCheLiangJiShuZhuangKuang"));
        data.put("XingCheAnQuanShiGuQingKuang",model.getStr("XingCheAnQuanShiGuQingKuang"));
        return data;
    }

    //关连电子档案
    public void dangAnWenJian(String muLuName,String fileName,String filePath,String SYS_DanWeiDianNaoBianHao ,
                              String SYS_DanWeiMingCheng ,String SYS_DanWeiDaiMa){
        //创建文件记录（电子档案）
    	errorLogModel fileModel=new errorLogModel();
        fileModel.set("Id",  IdUtil.randomUUID());
        fileModel.set("ZhuId","13b8805f-2dd9-46dd-ac10-e4d409ac1116");//车辆安全检查
        fileModel.set("WenJianZiLei",muLuName);
        fileModel.set("WenJianLeiBie","隐患排查与治理");
        fileModel.set("WenJianMingCheng",fileName);
        fileModel.set("WenJianLaiYuan","系统生成");
        fileModel.set("WenJianWeiZhi",filePath);
        fileModel.set("SYS_DanWeiDianNaoBianHao",SYS_DanWeiDianNaoBianHao);
        fileModel.set("SYS_DanWeiMingCheng",SYS_DanWeiMingCheng);
        fileModel.set("SYS_DanWeiDaiMa",SYS_DanWeiDaiMa);
        fileModel.set("GengXinShiJian",new Date());
        fileModel.set("BianHao",1);
        fileModel.save();
    }
}
